﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TT.HelpInfo;
using SQLHelper;
using Bill.BasDLL.ety;
using System.Data;

namespace Bill.BasDLL.dao
{
    public class daoOraBillProject : basClassDeclare
    {
        public daoOraBillProject(TT.Module.sysLogin.etyBaeSystemParamter basSysPara)
        {
            _sqlHelp = basSysPara.SqlHelp;
            sPortCode = basSysPara.PORT_CODE;
            sUserID = basSysPara.UserID;
        }

        public bool Insert(etyBillProject eyTrk)
        {
            eyTrk.PORT_CODE = sPortCode;
            eyTrk.LST_USER = sUserID;
            string sql = "insert into bill_project (PORT_CODE,CUSTOMER,PROJ_SEQ,SVC_TYPE,ITEM,CHRG_FLT1,CHRG_FLT2,CHRG_FLT3,CHRG_FLT4,CHRG_FLT5,CHRG_FLT6,CHRG_FLT9,LST_UPD_DATE,lst_mod,lst_user,SPC_MOD,chrg_flt13) "
                + "values(:PORT_CODE,:CUSTOMER,(select nvl(max(proj_seq),0)+1 proj_seq from bill_project),:SVC_TYPE,:ITEM,:CHRG_FLT1,:CHRG_FLT2,:CHRG_FLT3,:CHRG_FLT4,:CHRG_FLT5,:CHRG_FLT6,:CHRG_FLT9,SYSDATE,:LST_MOD,:LST_USER,:SPC_MOD,:CHRG_FLT13)";

            return _sqlHelp.ExecuteTrans(sql, eyTrk).Exec_Success;

            //IList<etyBillProject> ilTrk = new List<etyBillProject>();
            //ilTrk.Add(eyTrk);
            //_sqlHelp.BeginTrans();
            //daoResult rst = _sqlHelp.Execute<etyBillProject>(sql, ilTrk);
            //if (!rst.Exec_Success)
            //{
            //    _sqlHelp.Rollback();
            //    _sqlHelp.EndTrans();
            //    return false;
            //}
            //_sqlHelp.Commit();
            //_sqlHelp.EndTrans();
            //return true;
        }

        public bool Update(etyBillProject eyTrk)
        {
            eyTrk.PORT_CODE = sPortCode;
            eyTrk.LST_USER = sUserID;
            string sql = "update bill_project set customer=:customer,svc_type=:svc_type,item=:item,lst_mod=:lst_mod,lst_user=:lst_user,lst_upd_date=sysdate,SPC_MOD=:SPC_MOD,"
                + "CHRG_FLT1=:CHRG_FLT1,CHRG_FLT2=:CHRG_FLT2,CHRG_FLT3=:CHRG_FLT3,CHRG_FLT4=:CHRG_FLT4,CHRG_FLT5=:CHRG_FLT5,CHRG_FLT6=:CHRG_FLT6,CHRG_FLT9=:CHRG_FLT9,CHRG_FLT13=:CHRG_FLT13 "
                + " where port_code=:port_code and proj_seq=:proj_seq"; 

            return _sqlHelp.ExecuteTrans(sql, eyTrk).Exec_Success;

            //IList<etyBillProject> ilTrk = new List<etyBillProject>();
            //ilTrk.Add(eyTrk);
            //_sqlHelp.BeginTrans();
            //daoResult rst = _sqlHelp.Execute<etyBillProject>(sql, ilTrk);
            //if (!rst.Exec_Success)
            //{
            //    _sqlHelp.Rollback();
            //    _sqlHelp.EndTrans();
            //    return false;
            //}
            //_sqlHelp.Commit();
            //_sqlHelp.EndTrans();
            //return true;
        }

        public bool Delete(etyBillProject eyBillCode)
        {
            string sql = "delete from bill_project "
                + " where port_code=:port_code and proj_seq=:proj_seq";

            return _sqlHelp.ExecuteTrans(sql, eyBillCode).Exec_Success;

            //IList<etyBillProject> ilList = new List<etyBillProject>();
            //ilList.Add(eyBillCode);
            //_sqlHelp.BeginTrans();
            //daoResult rst = _sqlHelp.Execute<etyBillProject>(sql, ilList);
            //if (!rst.Exec_Success)
            //{
            //    _sqlHelp.Rollback();
            //    _sqlHelp.EndTrans();
            //    return false;
            //}
            //_sqlHelp.Commit();
            //_sqlHelp.EndTrans();
            //return true;
        }

        public DataTable GetBillProject()
        {
            string sql = string.Empty;
            //sql = "select distinct p.PORT_CODE,p.CUSTOMER,p.PROJ_SEQ,p.SVC_TYPE,p.ITEM,c.cust_name,r.code_desc svc_desc,b.item_desc "
            //    + " from bill_project p"
            //    + " left join customer c on p.port_code=c.port_code and p.customer=c.customer"
            //    + " left join code_ref r on p.svc_type=r.code and r.item in('BILL_FIX','DEF_METER')"
            //    + " left join bill_code b on p.port_code=b.port_code and p.item=b.item"
            //    + " where p.port_code=:port_code"
            //    + " order by p.customer,p.svc_type,p.item";
            sql = "select * from vw_bill_project where port_code=:port_code order by customer,svc_type,item";
            DataTable dt;
            dt = _sqlHelp.ExecuteReader(sql, new string[] { sPortCode });
            return dt;
        }
    }
}
